package com.deyuanyun.pic.ctrl.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.deyuanyun.pic.business.domain.MileagePoint;
import com.deyuanyun.pic.business.service.NagmStationService;
import com.deyuanyun.pic.common.spring.SpringAppliction;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.ctrl.domain.EqptCptestStation;
import com.deyuanyun.pic.ctrl.domain.EqptCptestStationExample;
import com.deyuanyun.pic.ctrl.mapper.CtrlBaseMapper;
import com.deyuanyun.pic.ctrl.mapper.EqptCptestStationMapper;
import com.deyuanyun.pic.ctrl.service.CtrlBaseService;
import com.deyuanyun.pic.settings.domain.util.DomainCollectionsUtil;

@Service
public class CtrlBaseServiceImpl implements CtrlBaseService {

    private @Resource EqptCptestStationMapper eqptCptestStationMapper;
    private @Resource CtrlBaseMapper ctrlBaseMapper;
    private @Resource NagmStationService nagmStationService;
    //private @Resource @Lazy CtrlBaseService ctrlBaseService;
    
    @Override
    public void updateNagmStation(String pipeLineId) {
        
        //查询出该管道下所有测试桩
        EqptCptestStationExample example = new EqptCptestStationExample();
        example.createCriteria().andDeletedEqualTo((short)0).andPipeLineIdEqualTo(pipeLineId);
        example.setOrderByClause("mileage");
        List<EqptCptestStation> list = eqptCptestStationMapper.selectByExample(example);
        List<MileagePoint> stationList = new ArrayList<MileagePoint>(list.size());
        for (int i = 0; i < list.size(); i++) {//放入MileagePoint
            EqptCptestStation eqptCptestStation = list.get(i);
            MileagePoint station = new MileagePoint();
            station.setId(eqptCptestStation.getId());
            station.setMileage(eqptCptestStation.getMileage());
            stationList.add(station);
        }


        //查询有附近测试桩的表
        for (int i = 0; i < NagmStationPrefixs.length; i++) {
            String prefix = NagmStationPrefixs[i];
            if(StringUtils.isNotEmpty(prefix)){
                prefix = prefix + "_";
            }
            //查询有附近测试桩的表名
            CtrlBaseService ctrlBaseService = SpringAppliction.getBean(CtrlBaseService.class);
            String[] tableNames = ctrlBaseService.queryTableColumns(prefix);//需要这么调用，要不然不会用到缓存
            for (int j = 0; j < tableNames.length; j++) {
                
                String tableName = tableNames[j];
                
                //查询需要依赖的原始数据绝对距离
                List<MileagePoint> dataMileagePointList = ctrlBaseMapper.queryMileage(tableName, prefix, pipeLineId);
                Long[] mileages = DomainCollectionsUtil.toOneFieldValuesToLong(dataMileagePointList, "mileage");
                MileagePoint[] nagmStationMileagePoints = nagmStationService.findNagmStation(stationList, mileages);
                
                Asserts.assertTrue(dataMileagePointList.size() == nagmStationMileagePoints.length, "查询附近测试桩返回的条数不完整！");
                for (int k = 0; k < nagmStationMileagePoints.length; k++) {//循环附近测试桩
                    MileagePoint nagmStationMileagePoint = nagmStationMileagePoints[k];//附近测试桩
                    MileagePoint dataMileagePoint = dataMileagePointList.get(k);//需要矫正的数据
                    
                    String id = dataMileagePoint.getId();
                    String nagmId = null;
                    Long nagmMileage = null;
                    if(nagmStationMileagePoint != null){
                        nagmId = nagmStationMileagePoint.getId();
                        nagmMileage = nagmStationMileagePoint.getMileage();   
                    }
                    //这个地方或许可以考虑批量处理什么的
                    ctrlBaseMapper.updateNagmStation(tableName, prefix, id, nagmId, nagmMileage);
                }
                
            }
            
        }
        

    }


    @Cacheable("foreveryCache")
    @Override
    public String[] queryTableColumns(String prefix) {
        //只是为了要一个缓存罢了。
        return ctrlBaseMapper.queryTableColumns(prefix);
    }

}
